/*
 * <p>$Id: //depot/MarketData/nds/main/src/main/java/com/ebs/md/nds/management/JMXTopicListener.java#2 $ </p>
 * <p>$Change: 75206 $ </p>
 * <p>$Author: chelck $ </p>
 * <p>$Date: 2009/07/16 $ </p>
 *
 * Copyright 2007 EBS Dealing Resources Inc.
 * All Rights Reserved
 */

package com.ebs.md.fxmonitor.management;

import java.util.concurrent.atomic.AtomicLong;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
import javax.management.ObjectName;

import org.apache.log4j.Logger;

import com.ebs.commons.util.SysDump;


import com.ebs.commons.comp.IParsedPayload;
import com.ebs.md.fxmonitor.topics.JmxTopicKeys;

public class JMXTopicListener extends NotificationBroadcasterSupport
                              implements MessageListener, MBeanRegistration, JMXTopicListenerMBean {
    private static final AtomicLong s_seqNo     = new AtomicLong();
    private static final Logger     s_logger    = Logger.getLogger(SysDump.getCallerClass());

    private ObjectName  m_objectName;


    public void onMessage(Message message) {
        s_logger.info("JMXTopicListener.onMessage(" + message + ")");
        try {
            IParsedPayload  payload         = extractPayload(message);
            String          notifactionType = extractNotificationType(payload);
            String          text            = payload.getParsedStringField(JmxTopicKeys.Text);

            Notification notification = new Notification("Application.Generic." + notifactionType, m_objectName, s_seqNo.addAndGet(1), System.currentTimeMillis(), text);
            sendNotification(notification);
        }
        catch (JMSException ex) {
            s_logger.error("Unable to transform message into JMX Notification: " + message, ex);
        }
    }

    public void postDeregister() {
        // TODO Auto-generated method stub

    }

    public void postRegister(Boolean registrationDone) {
        // TODO Auto-generated method stub

    }

    public void preDeregister() throws Exception {
        // TODO Auto-generated method stub

    }

    public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception {
        m_objectName = name;
        return name;
    }

    private IParsedPayload extractPayload(Message message) throws JMSException {
        IParsedPayload payload;

        if(message instanceof ObjectMessage) {
            ObjectMessage objMsg = (ObjectMessage) message;
            payload = (IParsedPayload) objMsg.getObject();
        }
        else {
            throw new JMSException("Unable to handle this message type " + message.getJMSType());
        }

        return payload;
    }

    private String extractNotificationType(IParsedPayload payload) {
        Object field = payload.getParsedField(JmxTopicKeys.NotificationType);
        String notificationType;

        if(field instanceof JmxTopicKeys.NotificationTypes) {
            JmxTopicKeys.NotificationTypes type = (JmxTopicKeys.NotificationTypes) field;
            notificationType = type.name();
        }
        else if(field instanceof String) {
            notificationType = (String) field;
        }
        else {
            notificationType = field.toString();
        }

        return notificationType;
    }

}
